package com.tsingteng.cosfun.ui.cosfun.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.bean.AttentFansbean;
import com.tsingteng.cosfun.bean.CommBean;
import com.tsingteng.cosfun.utils.DialogUtil;
import com.tsingteng.cosfun.utils.StringUtils;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class AtListAdapter extends RecyclerView.Adapter {
    public enum FooterState {
        Normal, Loading, TheEnd;
    }

    private final int FOOTERLAYOUT = 100;//加载更多
    Context context;
    public FooterHolder mFooterHolder;
    List<AttentFansbean.Data> commList = null;
    OnItemClickListener onItemClickListener = null;

    public AtListAdapter(Context context, List<AttentFansbean.Data> commList, AtListAdapter.OnItemClickListener onItemClickListener) {
        this.context = context;
        this.commList = commList;
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == 0) {
            view = LayoutInflater.from(context).inflate(R.layout.fragment_at_item, parent, false);
            return new AtHolder(view);
        } else {//加载更多
            view = LayoutInflater.from(context).inflate(R.layout.listview_footer, parent, false);
            mFooterHolder = new FooterHolder(view);
            return mFooterHolder;
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof AtListAdapter.AtHolder) {
            ((AtListAdapter.AtHolder) holder).setData(position);
        }
    }

    @Override
    public int getItemCount() {
        if (commList == null) {
            return 0;
        }
        return commList.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (getItemCount() > 0 && position == getItemCount() - 1) {
            return FOOTERLAYOUT;
        } else {
            return 0;
        }
    }

    //icon
    class AtHolder extends RecyclerView.ViewHolder {
        CircleImageView circleImageView = null;
        TextView author_tv;
        TextView des_tv;
        RelativeLayout rel = null;

        public AtHolder(View itemView) {
            super(itemView);
            circleImageView = itemView.findViewById(R.id.at_item_img);
            author_tv = itemView.findViewById(R.id.at_item_author);
            des_tv = itemView.findViewById(R.id.at_item_des);
            rel = itemView.findViewById(R.id.at_item_parent);
        }

        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        public void setData(int position) {
            AttentFansbean.Data data = commList.get(position);
            if (data.image != null)
                Glide.with(context).load(data.image).into(circleImageView);
            author_tv.setText(data.nickName);
            des_tv.setText(data.signature);
            rel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (data.beBlacked == 1) {
//                        Toast.makeText(context, "已拉黑不能@评论", Toast.LENGTH_SHORT).show();
                        @SuppressLint({"StringFormatInvalid", "LocalSuppress"})
                        String content = context.getString(R.string.comment_black,data.nickName);
                        DialogUtil.getContentDialog((Activity) context,content);
                        return;
                    }
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(position);
                    }
                }
            });
        }
    }

    public class FooterHolder extends RecyclerView.ViewHolder {
        View mLoadingViewstubstub;
        View mEndViewstub;
        public AtListAdapter.FooterState status = AtListAdapter.FooterState.Normal;

        public FooterHolder(View itemView) {
            super(itemView);
            mLoadingViewstubstub = itemView.findViewById(R.id.glistview_footer_content_rel);
            mEndViewstub = itemView.findViewById(R.id.glistview_footer_nomore_rel);
        }

        //根据传过来的status控制哪个状态可见
        public void setStatus(AtListAdapter.FooterState status) {
            this.status = status;
            switch (status) {
                case Normal:
                    setAllGone();
                    break;
                case Loading:
                    setAllGone();
                    mLoadingViewstubstub.setVisibility(View.VISIBLE);
                    break;
                case TheEnd:
                    setAllGone();
                    mEndViewstub.setVisibility(View.VISIBLE);
                    break;
                default:
                    break;
            }

        }

        //全部不可见
        void setAllGone() {
            if (mLoadingViewstubstub != null) {
                mLoadingViewstubstub.setVisibility(View.GONE);
            }
            if (mEndViewstub != null) {
                mEndViewstub.setVisibility(View.GONE);
            }
        }

    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }
}
